package com.ruoyi.safe.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.dto.CommonUserDTO;
import com.ruoyi.safe.domain.dto.UserSearchDTO;
import com.ruoyi.safe.domain.vo.CommonUserVO;
import com.ruoyi.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.safe.mapper.CommonUserMapper;
import com.ruoyi.safe.domain.CommonUser;
import com.ruoyi.safe.service.ICommonUserService;

/**
 * 项目人员管理Service业务层处理
 * 
 * @author buxi
 * @date 2023-11-08
 */
@Service
@AllArgsConstructor
public class CommonUserServiceImpl implements ICommonUserService 
{
    @Autowired
    private CommonUserMapper commonUserMapper;
    @Autowired
    private SysUserMapper userMapper;

    /**
     * 查询项目人员管理
     * 
     * @param id 项目人员管理主键
     * @return 项目人员管理
     */
    @Override
    public CommonUser selectCommonUserById(Long id)
    {
        return commonUserMapper.selectCommonUserById(id);
    }

    /**
     * 查询项目人员管理列表
     * 
     * @param userSearch 项目人员管理
     * @return 项目人员管理
     */
    @Override
    public List<CommonUserVO> selectCommonUserList(UserSearchDTO userSearch)
    {
        return commonUserMapper.selectCommonUserList(userSearch);
    }

    /**
     * 新增项目人员管理
     * 
     * @param projectId, userIds 项目人员管理
     * @return 结果
     */
    @Override
    public int insertCommonUser(Long projectId, Long[] userIds)
    {
        List<CommonUser> commonUsers = new ArrayList<>();
        for (Long userId : userIds) {
            CommonUser commonUser = new CommonUser();
            commonUser.setProjectId(projectId);
            commonUser.setUserId(userId);
            commonUser.setCreateBy(SecurityUtils.getUsername());
            commonUser.setCreateTime(DateUtils.getNowDate());
            commonUsers.add(commonUser);
        }
        return commonUserMapper.insertCommonUser(commonUsers);
    }

    /**
     * 修改项目人员管理
     * 
     * @param commonUser 项目人员管理
     * @return 结果
     */
    @Override
    public int updateCommonUser(CommonUser commonUser)
    {
        commonUser.setUpdateTime(DateUtils.getNowDate());
        commonUser.setCreateBy(SecurityUtils.getUsername());
        return commonUserMapper.updateCommonUser(commonUser);
    }

    /**
     * 批量删除项目人员管理
     * 
     * @param ids 需要删除的项目人员管理主键
     * @return 结果
     */
    @Override
    public int deleteCommonUserByIds(Long[] ids)
    {
        return commonUserMapper.deleteCommonUserByIds(SecurityUtils.getUsername(),ids);
    }

    /**
     * 删除项目人员管理信息
     * 
     * @param id 项目人员管理主键
     * @return 结果
     */
    @Override
    public int deleteCommonUserById(Long id)
    {
        return commonUserMapper.deleteCommonUserById(SecurityUtils.getUsername(),id);
    }

    @Override
    public List<SysUser> selectCommonUserList(CommonUserDTO commonUserDTO) {
        return userMapper.notInCommonUserList(commonUserDTO);
    }
}
